package com.lisp.springframework.core.aop.aspectj.annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.EnableLoadTimeWeaving.AspectJWeaving;
import org.springframework.context.annotation.PropertySource;

// run with vm args "-javaagent:spring-instrument.jar" 
@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
@EnableAspectJAutoProxy
@PropertySource("classpath:app.properties")
@ComponentScan(basePackages = "com.lisp.springframework.core.aop.aspectj.annotation")
public class AspectJAnnotationApp {

    public static void main(String[] args) {
        try(AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();) {            
            ctx.register(AspectJAnnotationApp.class);
            ctx.refresh();
            
            // MyServiceA被定义时，还没有添加transform，所以都MyServiceA无效
            new MyServiceA().sayHello("new myServiceA"); 
            
            MyServiceA myServiceA = ctx.getBean(MyServiceA.class);
            myServiceA.sayHello("mySerivceA from spring");
            myServiceA.print("mySerivceA from spring");

            NonSpringManaged nsm = new NonSpringManaged();
            nsm.sayHello("nonSpringManaged");
            
            // 内部转调可以触发切面
            InnerInvoke ii = new InnerInvoke();
            ii.sayHello("innerInvoke");
        }
    }
}
